﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using Summer.Exception;
using Summer.Object;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.IO
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009-11-09 13:46:15
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.IO
{
    /// <summary>
    /// Define some method about assembly
    /// </summary>
    public static class AssemblyManagement
    {
        /// <summary>
        /// Load library from a string
        /// The string format is like "XXX.XX.XXClass,XXX.dll" or "XXX.XX.XXClass"
        /// The file should exist or an exception would be thrown
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static object LoadLibrary(this string data)
        {
            if (data == null || data.Trim() == string.Empty) { return null; }
            if (data.IndexOf(',') == -1)
            {
                return Assembly.GetExecutingAssembly().CreateInstance(data);
            }
            string className = data.Substring(0, data.IndexOf(','));
            string classAssembly = data.Substring(data.IndexOf(',') + 1);
            classAssembly = classAssembly.FindPath(Const.DotNetRoot().ToArray(),new Summer.Exception.IOException("Assembly Cannot Find"));
            try
            {
                object o=Assembly.LoadFrom(classAssembly).CreateInstance(className);
                if (o != null) { return o; }
                throw new Summer.Exception.IocException("Assembly Load Null Pointer");
            }
            catch(System.Exception e)
            {
                throw e;
            }
        }
    }
}
